import base64
import json
import time
from urllib import parse

import requests
from requests import request

from pc29 import monitor28
from pc29.monitor28 import Player28, Recording, lottery

cookie="JSESSIONID=6129D6591B73A3656F8D213A295A9FF0; hyfbm=63047; SESSION=9e277555-631a-49a4-b94e-12c2461b634b"

ggcNo='46010100'

ggcMap={
    "大":"1",
    "小":"2",
    "单":"3",
    "双":"4"
}


def lottery(moneyData,ggc):
    shuang = """
     [{
    	"wfmc2": "总和/龙虎和",
    	"wfmc3": "双",
    	"wfbm3": "460101004",
    	"hm": "",
    	"money": "1",
    	"pl": "1.995",
    	"tzhm": "双",
    	"zs": 1,
    	"price": "1"
    }]
     """

    m1="mOqkr5HB4v5drM5HmEtusNtTECaggHaPTRKUGX9kNRB9T8iV3vI3OdXiT+JGTea1TpiqaISSfZDEIU3ypBxJTvTtrysbKE8Zj7zZnw58JiwO7YNQAK3XMy5v7bX++WfKM2CDdu3+I942OgjWf6+CLWiM9SYgW6fsKLiKQCLof/A="
    m5="LYhgWC9nEfULRS3AHOGH0OrAE8X9QG5SEVeCIqk29tFnFsY9nA3mxV/B/T7wOglg0suU/EHyfguxUFTWiDGy00bJKQ2akyuUOceOI14gbwToZb5ZGfsU3b6Qkb9EFEykIquWz/OCILSXXPGDRKlSwIFEjDjLxIs3sEl34ECYnOI="
    cpbm="JvVkBzK9ARGB4amA6c8RtAG0FmshYbjhzaMCQ57E5VDMmytp7kgqyBg+I5DyI+0e0/WDU2NZtNBx6NdqouLTO4Dai0ga3pIbUOtEb506FWt9A0JSjuojMr5n1hDo963bMzZXcDj1+vJH2GruncWdy7YDonvQ3B8ZDQDykhJMO+A="
    wfbm1="HjnlXRo7pH9euC0odAgFb03qGRM8b2RRamkTAgTyhmnRadcgzQH6DruQc6ewf1uI7a6FsnpuwcFlhvNJFnK1PL7ht/H7BUY4k8c522yWpuwe2K9DslI9JzZq7j/N0ov4LQJbP4RzPtVZipKoZTtRYVGYGb+dL2cHUGR18LBAPi4="
    if moneyData!=1:
        m1=m5
    j=json.loads(shuang)
    j[0]['money'] = moneyData
    j[0]['price'] = j[0]['money']
    j[0]['wfbm3']=ggcNo+ggcMap[ggc]
    j[0]['wfmc3']=ggc
    j[0]['tzhm']=j[0]['wfmc3']
    res2 = json.dumps(j, indent=2, ensure_ascii=False)
    res2 = parse.quote(res2)
    res2= base64.b64encode(bytes(res2,encoding='utf-8'))
    res2=str(res2,encoding='utf-8')
    headers = {
        'user-agent': 'Mozilla/5.0',
        'Cookie':cookie
    }
    r = requests.post('https://www-pc44.com/ggc/save.do',
                      data={
                          "jsonrow": res2,
                          "tzzje": m1,
                          "cpbm": cpbm,
                          "cpqh": False,
                          "wfbm1": wfbm1,
                        },
                      headers=headers)
    rs=r.text.split('|')
    if len(rs) > 1:
        return rs[-1]!='0.000'


def lotteryBoot():
    headers = {
        'user-agent': 'Mozilla/5.0',
        'Cookie': cookie
    }
    money = requests.post("https://www-pc44.com/lottery/getHyje.do", headers=headers).text
    low=0
    bet=1
    ggc=['双','单','大','小']
    while float(money)>15.0:
        try:
            if  lottery(bet,"双"):
                bet = 5
                money = requests.post("https://www-pc44.com/lottery/getHyje.do", headers=headers).text
                print("剩余金额:"+money)
            elif float(money)>=90.0:
                break
            else:
                bet=1
                low+=1
                if low>=3:
                    low=0
                print("剩余金额:" + str(float(money)-float(low)))
        except Exception:
            pass
        time.sleep(1)
    print("输了"+str(low))
    return  money

def pc28():
    data=monitor28.lottery()
    cpqh=data.get('CPQH')
    print(cpqh)
    recordings=Recording.select().where(Recording.cpqh==cpqh)
    for r in recordings:
        p=Player28.get(Player28.id==r.user,Player28.total>30)
        print(p.rate)




if __name__ == '__main__':
    # print(lottery(5,'双'))
    # print(lotteryBoot())
    pc28()